package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.domain.entity.MallMessage;
import com.tencreat.domain.mall.MallMessageConfig;
import com.tencreat.domain.mall.vo.MallMessageListVO;
import com.tencreat.domain.mall.dto.MallMessageApiRecordDTO;
import com.tencreat.service.mall.IMallMessageConfigService;
import com.tencreat.service.mall.IMallMessageListService;
import com.tencreat.service.mall.IMallMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息聊天
 * @author zixin
 */
@RestController
@RequestMapping("/mall/message")
@Api(tags = "消息聊天")
public class MallMessageController {

    @Autowired
    private IMallMessageService messageService;

    @Autowired
    private IMallMessageListService messageListService;

    @Autowired
    private IMallMessageConfigService mallMessageConfigService;

    @ApiOperation("获取消息列表")
    @GetMapping("/list")
    HttpResult<List<MallMessageListVO>> list(Integer isToday) {
        return HttpResult.ok(messageService.selectList(isToday,null));
    }

    @ApiOperation("获取聊天消息记录")
    @PostMapping("/record")
    HttpResult<Page<MallMessage>> record(@RequestBody @Validated MallMessageApiRecordDTO vo) {
        Page page = new Page(vo.getCurrent(), vo.getSize());
        return HttpResult.ok(messageService.pageMessage(page, vo.getUuid(), vo.getTime()));
    }

    @ApiOperation("清除未读消息")
    @GetMapping("/clear-unread/{uuid}")
    HttpResult clearUnread(@PathVariable("uuid") Long uuid) {
        messageService.clearUnreadMessage(uuid);
        return HttpResult.ok();
    }

    @ApiOperation("删除消息列表")
    @GetMapping("/remove/{id}")
    HttpResult remove(@PathVariable("id") Long id) {
        messageListService.removeById(id);
        return HttpResult.ok();
    }

    @ApiOperation("获取消息配置")
    @GetMapping("/messageConfig")
    HttpResult<MallMessageConfig> getMessageConfig(){
        MallMessageConfig messageConfig = mallMessageConfigService.lambdaQuery().last("limit 1").one();

        return HttpResult.ok(messageConfig);
    }

    @ApiOperation("修改消息配置")
    @PostMapping("/updateMessageConfig")
    HttpResult<Boolean> updateMessageConfig(@RequestBody MallMessageConfig mallMessageConfig){
        return HttpResult.ok(mallMessageConfigService.updateById(mallMessageConfig));
    }

    @ApiOperation("今日接待数量统计")
    @GetMapping("/receptionToday")
    HttpResult<Integer> receptionToday(){
        return HttpResult.ok(messageService.receptionToday());
    }

}
